package me.zhengjie.spider.util.processor;

import lombok.Data;
import me.zhengjie.spider.common.SpiderConstant;
import me.zhengjie.spider.domain.video.VideoBean;
import me.zhengjie.spider.domain.video.VideoRuleBean;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.processor.PageProcessor;

@Data
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SpiderVideoDetailPageProcessor implements PageProcessor {

    private VideoBean bean;
    private VideoRuleBean ruleBean;

    @Override
    public void process(Page page) {
        if (null != this.ruleBean.getPlayLink()) {
            page.putField(SpiderConstant.PLAY_LINK, page.getHtml().regex(this.ruleBean.getPlayLink()).toString());
        }
        if (null != this.ruleBean.getType()) {
            page.putField(SpiderConstant.TYPE, page.getHtml().xpath(this.ruleBean.getType()).toString());
        }
        if (null != this.ruleBean.getTag()) {
            page.putField(SpiderConstant.TAG, page.getHtml().xpath(this.ruleBean.getTag()).all());
        }
    }
}
